<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Send letter' );

$to = "";
$text = '';
if (isset ($_GET['to'])) {
	$to = formatInput($_GET['to']);
	$query = "SELECT count(*) FROM ruler WHERE name = '" . $to . "'";
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if ($row['count(*)'] == 0) {
		$to = "";
	}
}
elseif (isset ($_SESSION['send_letter_to'])) {
	$to = $_SESSION['send_letter_to'];
	unset( $_SESSION['send_letter_to'] );
}

if (isset ($_POST['message_id'])) {
	$reply = $_POST['message_id'];
	$query = "SELECT name FROM ruler WHERE ruler_id = " . $_SESSION['ruler_id'];
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$name = $row['name'];
	$query = "SELECT subject, sender, text, date, message_id FROM messages WHERE message_id = " . $_POST['message_id'];
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	if (substr($row['subject'], 0, 3) != "Re:") {
		$subject = "Re: " . stripslashes($row['subject']);
	} else {
		$subject = stripslashes($row['subject']);
	}
	$parent = "<a href=\"letter.php?id=" . $row['message_id'] . "#focus\"><strong>Subject: " . $row['subject'] . "</strong></a><br />\r\n";
	$parent .= "Date: " . $row['date'] . ", From: ";
	$query4 = "SELECT name FROM ruler WHERE ruler_id = " . $row['sender'];
	$result4 = mysql_query($query4) or die("Query failed : " . mysql_error());
	$row4 = mysql_fetch_array($result4, MYSQL_ASSOC);
	$parent .= $row4['name'] . "<br />";
	$parent .= "<pre>" . wordwrap(stripslashes($row['text']), 80) . "</pre>";
	$parent .= "<div class=\"main-message\">\r\n";
	$query = "SELECT name FROM ruler JOIN message_ruler ON ruler.ruler_id = message_ruler.ruler_id WHERE message_id = " . $_POST['message_id'];
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$num = mysql_numrows($result);
	$i = 0;
	while ($i < $num) {
		if ($name != mysql_result($result, $i, "name")) {
			if ($to == "") {
				$to = mysql_result($result, $i, "name");
			} else {
				$to = $to . ", " . mysql_result($result, $i, "name");
			}
		}
		$i++;
	}
	$query = "SELECT name FROM ruler WHERE ruler_id = " . $row['sender'];
	$result = mysql_query($query) or die("Query failed : " . mysql_error());
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	//$name = $row[ 'name' ];
	if ($name != $row['name']) {
		if ($to == "") {
			$to = $row['name'];
		} else {
			$to = $to . ", " . $row['name'];
		}
	}
} 
else {
	$reply = -1;
	$subject = "";
}

if( isset( $_SESSION[ 'send_letter_subject' ] ) ) {
	$subject = $_SESSION[ 'send_letter_subject' ];
	unset( $_SESSION[ 'send_letter_subject' ] );
}

if( isset( $_SESSION[ 'send_letter_text' ] ) ) {
	$text = $_SESSION[ 'send_letter_text' ];
	unset( $_SESSION[ 'send_letter_text' ] );
}
?>
					<div class="blok">
<?php
printDiplomacyTab( 'Send a letter' );
?> 
					</div>
					<br />
					<div class="message">
<?php
if (isset ($parent)) {
	print ($parent);
}
if (isset ($_SESSION['send_letter_status'])) {
	print translate('Status') . ": " . $_SESSION['send_letter_status'] . "<br /><br />\r\n";
	unset ($_SESSION['send_letter_status']);
}
print "<strong>" . translate('Send a letter') . ":</strong><br/>\r\n";
print "<form method=\"post\" action=\"send_letter2.php\" accept-charset=\"UTF-8\">\r\n";
print "<table class=\"message-header\">";
print "<tr><td>" . translate('To') . ":</td><td><input type=\"text\" size=\"56\" name=\"to\" value=\"$to\" /></td></tr>\r\n";
$query = "SELECT count(*) FROM faction_member WHERE ruler_id = " . $_SESSION['ruler_id'] . " AND requested = 0 AND invited = 0";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if ($row['count(*)'] > 0) {
	print "<tr><td>" . translate('To faction') . ":</td><td><input type=\"checkbox\" name=\"faction\" /></td></tr>\r\n";
}
print "<tr><td>" . translate('Subject') . ":</td><td><input type=\"text\" size=\"56\" name=\"subject\" value=\"$subject\"/></td></tr></table><br />\r\n";
print "<textarea charset=\"UTF-8\" name=\"text\" cols=\"65\" rows=\"12\">$text</textarea><br />\r\n";
print "<input type=\"hidden\" value=\"$reply\" name=\"reply\" />\r\n";
print "<input type=\"submit\" value=\"" . translate('send') . "\"/><br />\r\n";
print "</div>" .
		"</div>";
aos_footer();
?>
